
var template;

var selectedRows=null;  //记录选中的元素
var dictionaryConfigId=null;

function initPage()
{
}

function cancel()
{
	template.actionClass.add(dictionaryConfigId,template._edit);
}

function init()
{
	//用现有模板类实现大部分的操作
	template=new SimpleTemplate();
	template.actionClass=DictionaryAction;
	template.dataGridListCtrl=dataGridListCtrl;
	template.editPart=editPart;
	template.listFields=new Array("id","code","name","active","remark");
	template.validate=new ValidateConfig('dictionary.check.xml');

	template.initPage=initPage;  //改写实例中的方法，相当于重载
	template.cancel=cancel;

	template.init();
}


function loadAllDictionaryConfig()
{
	DictionaryAction.allDictionaryConfig(_loadAllDictionaryConfig);
}

function _loadAllDictionaryConfig(data)
{
	if(data!=null && data.length>0)
	{
		for(var i=0;i<data.length;i++)
		{
			$('dictConfigSelect').options[i]=new Option(data[i][1]+"("+data[i][2]+")",data[i][0]);
		}
	}
}


function selectDictConfig()
{
	var select=$('dictConfigSelect');
	var form=$('editForm');

	dictionaryConfigId=select.options[select.selectedIndex].value;
	$('promptMsg').innerHTML=select.options[select.selectedIndex].text.htmlEncode();

	DictionaryAction.add(select.getValue(),template._edit);
	DictionaryAction.query(select.getValue(),template._query);
	editPart.show();
}

function dataGridSelectedEvent(dataGrid,line,isSelected)
{
	if(dataGrid.selectedRows.length>0)
	{
		$("deleteButton").disabled=false;
		$("upButton").disabled=false;
		$("downButton").disabled=false;

		if(dataGrid.selectedRows.length==1)
			$("modifyButton").disabled=false;
		else
			$("modifyButton").disabled=true;
	}
	else
	{
		$("deleteButton").disabled=true;
		$("upButton").disabled=true;
		$("downButton").disabled=true;
		$("modifyButton").disabled=true;

		if(selectedRows!=null)
		{
			for(var i=0;i<selectedRows.length;i++)
			{
				dataGrid.selectLine(selectedRows[i]);
			}

			selectedRows=null;
		}
	}
}

function initDataGrid()
{
	dataGrid.appendField(new DataGridField("",50,DataType.lineNumber));
	dataGrid.appendField(new DataGridField("Code",120));
	dataGrid.appendField(new DataGridField("Name",120));
	dataGrid.appendField(new DataGridField("Active",50,DataType.Boolean));
	dataGrid.appendField(new DataGridField("Remark",200));
	dataGrid.selectedEvent=dataGridSelectedEvent;
	dataGrid.init(240);
}

function moveUp()
{
	if(dictionaryConfigId!=null && dataGrid.selectedRows.length>0)
	{
		var ids=new Array();
		for(var i=0;i<dataGrid.selectedRows.length;i++)
		{
			ids.push(dataGrid.getValue(dataGrid.selectedRows[i],0));
		}

		DictionaryAction.moveUp(dictionaryConfigId,ids,_moveUp,dataGrid.selectedRows);
	}
}

function _moveUp(data,_selectedRows)
{
	dataGridListCtrl.reload();
	selectedRows=new Array();
	for(var i=0;i<_selectedRows.length;i++)
	{
		selectedRows.push(_selectedRows[i]-1);
	}
}

function moveDown()
{
	if(dictionaryConfigId!=null && dataGrid.selectedRows.length>0)
	{
		var ids=new Array();
		for(var i=0;i<dataGrid.selectedRows.length;i++)
		{
			ids.push(dataGrid.getValue(dataGrid.selectedRows[i],0));
		}

		DictionaryAction.moveDown(dictionaryConfigId,ids,_moveDown,dataGrid.selectedRows);
	}
}

function _moveDown(data,_selectedRows)
{
	dataGridListCtrl.reload();
	selectedRows=new Array();
	for(var i=0;i<_selectedRows.length;i++)
	{
		selectedRows.push(_selectedRows[i]+1);
	}
}

initEvents.push("editPart.hidden();");
initEvents.push(loadAllDictionaryConfig);
initEvents.push(initDataGrid);
initEvents.push(init);






